package com.ruoyi.ap.controller;

import com.ruoyi.ap.domain.Plant;
import com.ruoyi.ap.pojo.vo.PlantVo;
import com.ruoyi.ap.service.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 种植情况Controller
 * 
 * @author HuaQP
 * @date 2023-09-11
 */
@RestController
@RequestMapping("/ap/plant")
public class PlantController extends BaseController
{
    @Resource
    private IPlantService plantService;

    @Resource
    private IProductCategoryService categoryService;

    @Resource
    private IVillageService villageService;

    @Resource
    private IFarmerService farmerService;

    /**
     * 查询种植情况列表
     */
    @PreAuthorize("@ss.hasPermi('ap:plant:list')")
    @GetMapping("/list")
    public TableDataInfo list(PlantVo plantVo)
    {
        startPage();
        List<PlantVo> list = plantService.selectPlantVoList(plantVo);
        return getDataTable(list);
    }

    /**
     * 导出种植情况列表
     */
    @PreAuthorize("@ss.hasPermi('ap:plant:export')")
    @Log(title = "种植情况", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PlantVo plantVo)
    {
        List<PlantVo> list = plantService.selectPlantVoList(plantVo);
        ExcelUtil<PlantVo> util = new ExcelUtil<>(PlantVo.class);
        util.exportExcel(response, list, "种植情况数据");
    }

    /**
     * 获取种植情况详细信息
     */
    @PreAuthorize("@ss.hasPermi('ap:plant:query')")
    @GetMapping(value = {"/", "/{id}"})
    public AjaxResult getInfo(@PathVariable(value="id",required = false) Long id)
    {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("categorys",categoryService.selectCategoryAll());
        ajax.put("villages",villageService.selectVillageAll());
        if (StringUtils.isNotNull(id))
        {
            ajax.put(AjaxResult.DATA_TAG, plantService.selectPlantById(id));
        }
        return ajax;
    }

    /**
     * 新增种植情况
     */
    @PreAuthorize("@ss.hasPermi('ap:plant:add')")
    @Log(title = "种植情况", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Valid Plant plant)
    {
        return toAjax(plantService.insertPlant(plant));
    }

    /**
     * 修改种植情况
     */
    @PreAuthorize("@ss.hasPermi('ap:plant:edit')")
    @Log(title = "种植情况", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @Valid Plant plant)
    {
        return toAjax(plantService.updatePlant(plant));
    }

    /**
     * 删除种植情况
     */
    @PreAuthorize("@ss.hasPermi('ap:plant:remove')")
    @Log(title = "种植情况", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(plantService.deletePlantByIds(ids));
    }
}
